package com.cungmua.app.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.base.BaseDrawerActivitys;
import com.cungmua.app.handler.DealDetailsHandler;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.DealDetails;
import com.cungmua.app.model.request.DealDetailsRequest;
import com.cungmua.app.model.response.DealDetailsResponse;
import com.cungmua.app.task.DealDetailsTask;
import com.cungmua.app.utils.AppUtils;

/**
 * Created by kelvin.nguyen on 6/27/14.
 */
public class FeatueredActivity extends BaseDrawerActivitys implements DealDetailsHandler{
    private int currentDealId;
    private ImageView icon_home;
    private ImageView btn_logo;
    private Button icon_cart;
    private TextView txtTitelFeatured;
    private TextView txtHighLight;
    private WebView txtContent;
    private LinearLayout btnCart;
    private String titleHighLight;
    private TextView txtMarketPrice;
    private TextView txtPrice;
    private TextView txt_header_title;
    private ImageButton btnBuy;
    private DealDetails details;
    private String content="";

    @Override
    public int getLayoutResource() {
        return R.layout.activity_featuered;
    }

    public FeatueredActivity() {
    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        super.loadControls(savedInstanceState);
        currentDealId = getIntent().getExtras().getInt(IK_DEAL_ID);
        titleHighLight=getIntent().getExtras().getString(KEY_TITLE);

        txtMarketPrice = (TextView) findViewById(R.id.txtMarketPrice);
        txtMarketPrice.setPaintFlags(txtMarketPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        txtPrice = (TextView) findViewById(R.id.txtPrice);
        btnBuy = (ImageButton) findViewById(R.id.btnBuy);
        icon_home=(ImageView)findViewById(R.id.icon_home);
        btn_logo=(ImageView)findViewById(R.id.btn_logo);
        icon_cart=(Button)findViewById(R.id.icon_cart);
        txtTitelFeatured=(TextView)findViewById(R.id.txtTitelFeatured);
        txtHighLight=(TextView)findViewById(R.id.txtHighLight);
        txt_header_title=(TextView)findViewById(R.id.txt_header_title);
        txtContent=(WebView)findViewById(R.id.txtContent);
        btnCart=(LinearLayout)findViewById(R.id.btnCart);

        Cart currentCart=getCurrentCart();
        icon_cart.setText(currentCart.getTotalQuantity()+"");

        icon_home.setVisibility(View.GONE);
        btnCart.setVisibility(View.GONE);
        hideMenu();
        icon_home.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToYourCart();
            }
        });

        txt_header_title.setVisibility(View.VISIBLE);
        btn_logo.setVisibility(View.GONE);
    }

    @Override
    public void updateView() {
        DealDetailsRequest request = new DealDetailsRequest(getDeviceId(), getToken());
        request.setCityId(getCurrentCity().getId());
        request.setUserId(getCurrentUser().getId());
        request.setDealId(currentDealId);

        DealDetailsTask task = new DealDetailsTask(this, this);
        task.execute(request);
    }

    @Override
    public void onDealDetails(DealDetailsResponse response) {

        details = response.getData();

        String priceString = getString(R.string.full_price_string, AppUtils.getFullPriceString(details.getMarketPrice()));
        txtMarketPrice.setText(priceString);
        priceString = getString(R.string.full_price_string, AppUtils.getFullPriceString(details.getPrice()));
        txtPrice.setText(priceString);

     //   txtTitelFeatured.setText(titleHighLight+"");

        if(KEY_FEATURED.equalsIgnoreCase(titleHighLight)){
            content=details.getHighlight();
            content="<div class='detail_app_hi'>"+content+"</div>";
        }else if(KEY_NOTE.equalsIgnoreCase(titleHighLight)){
            content=details.getCondition();
            content="<div class='detail_app_hi'>"+content+"</div>";
        }else{
            content=details.getDescription();
            content="<div class='detail_app_content'>"+content+"</div>";
        }

        // String temp = details.getDescription();
        if (content == null)
            content = "";



        StringBuilder sb = new StringBuilder(content);
        sb.insert(0, "<html><head>");
//        sb.insert(0, "<style>img { max-width: 100%; height: auto; }</style>");
        sb.insert(0, "<style>div.detail_app_hi{ text-align:left; margin:0px; padding:0px;text-align:left; color:#555 !important; line-height:18px;}\n" +
                "div.detail_app_hi a{ color:#38B04A;} \n" +
                "div.detail_app_hi a:hover{ text-decoration:underline;}\n" +
                "div.detail_app_hi table{ border-collapse:collapse; width:100% !important;}\n" +
                "div.detail_app_hi table td{ border:1px solid #ccc; padding:3px;}\n" +
                "div.detail_app_hi ul, ul.list_su{list-style-image:url(http://www.cungmua.com/Content2014/images/arows_small.png); margin:10px 0px 0px 22px; list-style-position:outside;padding:0px 0px 0px 0px;color:#555!important; font-size:14px!important;} \n" +
                "div.detail_app_hi span[style] {font-size:14px!important;}\n" +
                "div.detail_app_hi li{ padding:0px 0px 8px 0px; }\n" +
                "div.detail_app_hi p{ margin:10px 0px 0px 20px; padding:0px 0px 5px 0px;color:#555 !important;font-size:14px!important; } \n" +
                "div.detail_app_hi ul ul{ list-style:disc !important; margin:10px 0px 0px 13px; list-style-position:inside;padding:0px 0px 0px 20px;color:#555!important; font-size:14px!important;} \n" +
                "div.detail_app_hi ul ul span[style] {font-size:14px!important;}\n" +
                "div.detail_app_hi li{ padding:0px 0px 8px 0px; }\n" +
                "div.detail_app_content{ text-align:justify;  color:#1e1e1d !important; font-size:14px!important; margin:0px; padding:0px; line-height:18px;}\n" +
                "div.detail_app_content span[style]{ font-size:14px!important;}\n" +
                "div.detail_app_content p{ text-align:justify;  color:#1e1e1d !important; }\n" +
                "div.detail_app_content img{ margin-top:10px;margin-bottom:10px; width:100%; height:auto; }\n" +
                "div.detail_app_content table{ width:100% !important;}\n" +
                "div.detail_app_content a{ color:#0f87aa; text-decoration:none;}\n" +
                "div.detail_app_content a:hover{ color:#0f87aa; text-decoration:underline;}</style>");
        sb.insert(0, "</head><body>");
        sb.append("</body></html>");
        txtContent.loadData(sb.toString(), "text/html; charset=UTF-8", null);
        txtContent.setPadding(0, 0, 0, 0);
        txtContent.setHorizontalScrollBarEnabled(false);

        //txtHighLight.setText(Html.fromHtml(content + ""));
        txt_header_title.setText(titleHighLight);



        switch (details.getDealStatus()) {
            case DealDetails.ACTIVE:
                btnBuy.setImageResource(R.drawable.selector_button_buy);
                break;
            case DealDetails.COMING_SOON:
                btnBuy.setImageResource(R.drawable.selector_button_coming_soon);
                break;
            case DealDetails.SOLD_OUT:
                btnBuy.setImageResource(R.drawable.selector_button_out_of_stock);
                break;
            case DealDetails.EXPIRED:
                btnBuy.setImageResource(R.drawable.selector_button_expired);
                break;
            case DealDetails.INACTIVE:
                btnBuy.setImageResource(R.drawable.selector_button_cancelled);
                break;
        }
        if (details.getDealStatus() != DealDetails.ACTIVE)
            btnBuy.setEnabled(false);

        btnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToBuy(details);
            }
        });
    }
}
